package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsPermissionService;
import com.qf.service.DtsRoleService;
import com.qf.util.JacksonUtil;
import com.qf.util.Permission;
import com.qf.util.PermissionUtil;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.PermVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/role")
@CrossOrigin
public class RoleController {

    @Autowired
    private DtsRoleService dtsRoleService;

    @Autowired
    private ApplicationContext context;

    @Autowired
    private DtsPermissionService dtsPermissionService;

    String basikPackage = "com.qf";
    @GetMapping("/options")
    public Object options(){
        List<CatVo> catVoList = dtsRoleService.findAll();

        return ResponseUtil.ok(catVoList);
    }
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "添加")
    @RequiresPermissions("admin:role:create")
    @PostMapping("/create")
    public Object create(@RequestBody DtsRole dtsRole){
        dtsRoleService.create(dtsRole);
        return ResponseUtil.ok(dtsRole);
    }
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "查询")
    @RequiresPermissions("admin:role:list")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page
            , @RequestParam(defaultValue = "20") Integer limit
            , @RequestParam(defaultValue = "add_time") String sort
            ,@RequestParam(defaultValue = "desc") String order){
        IPage<DtsRole> iPage = dtsRoleService.selectPage(page,limit,sort,order);
        long total = iPage.getTotal();
        List<DtsRole> records = iPage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("items", records);
        return ResponseUtil.ok(map);
    }
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "授权")
    @RequiresPermissions("admin:role:permissions")
    @GetMapping("/permissions")
    public Object permissions(Integer roleId){
        Map map = new HashMap();
        String basikPackage = "com.qf";
        List<Permission> permissions = PermissionUtil.listPermission(context, basikPackage);


        Set<String> permSet =  dtsPermissionService.findPermissionByRoleId(roleId);

        List<PermVo> permVoList = PermissionUtil.listPermVo(permissions);

        List<String> assignedPermissions = permissions.stream().map(permission -> {
            return permission.getRequiresPermissions().value()[0];
        }).collect(Collectors.toList());

        if (permSet.contains("*")){
            map.put("assignedPermissions", assignedPermissions);
        }else {
            Set<String> permRoleId = permSet.stream().filter(perm -> {
                if (assignedPermissions.contains(perm)) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toSet());
            map.put("assignedPermissions", permRoleId);

        }
        map.put("systemPermissions", permVoList);

        return ResponseUtil.ok(map);
    }


    @PostMapping("/permissions")
    public Object permissions(@RequestBody String body){
        List<String> permissions = JacksonUtil.parseStringList(body, "permissions");
        Integer roleId = JacksonUtil.parseInteger(body, "roleId");
        dtsPermissionService.updatePermissions(roleId,permissions);
        return ResponseUtil.ok();
    }




    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "更新")
    @RequiresPermissions("admin:role:update")
    @PostMapping("/update")
    public Object update(@RequestBody DtsRole dtsRole){
        dtsRoleService.update(dtsRole);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"系统管理","角色管理"},button = "删除")
    @RequiresPermissions("admin:role:delete")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsRole dtsRole){
        dtsRoleService.delete(dtsRole);
        return ResponseUtil.ok();
    }


}
